package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.persistence.*;

import play.db.jpa.Model;

@Entity
public class User extends Model 
{	
	@Column(unique = true) 
	private String facebookID;
	
	private String name;
	private String profilPicture;
	
	@ManyToMany(cascade = CascadeType.ALL)
	private List<Serie> follows;
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Rate> rates;
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Comment> comments;
	
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Topic> topicList;
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Video> videoList;
	
	
	
	

	public User(String facebookID, String name, String profilPicture,
			List<Serie> follows, List<Rate> rates, List<Comment> comments,
			List<Topic> topicList, List<Video> videoList) {
		super();
		this.facebookID = facebookID;
		this.name = name;
		this.profilPicture = profilPicture;
		this.follows = new ArrayList<Serie>();
		this.rates = new ArrayList<Rate>();
		this.comments = new ArrayList<Comment>();
		this.topicList = new ArrayList<Topic>();
		this.videoList = new ArrayList<Video>();
	}


	public User(String facebookID, String name, String profilPicture,
			List<Serie> follows, List<Rate> rates, List<Comment> comments,
			List<Topic> topicList) {
		super();
		this.facebookID = facebookID;
		this.name = name;
		this.profilPicture = profilPicture;
		this.follows = new ArrayList<Serie>();
		this.rates = new ArrayList<Rate>();
		this.comments = new ArrayList<Comment>();
		this.topicList = new ArrayList<Topic>();
		this.videoList = new ArrayList<Video>();
	}


	public User(String facebookID, String name, String profilPicture) {
		super();
		this.facebookID = facebookID;
		this.name = name;
		this.profilPicture = profilPicture;
		this.follows = new ArrayList<Serie>();
		this.rates = new ArrayList<Rate>();
		this.comments = new ArrayList<Comment>();
		this.videoList = new ArrayList<Video>();
	}


	public User(String facebookID) 
	{
		this.facebookID = facebookID;
		this.follows = new ArrayList<Serie>();
		this.rates = new ArrayList<Rate>();
		this.comments = new ArrayList<Comment>();
		this.videoList = new ArrayList<Video>();

	}
	

	public List<Video> getVideoList() {
		return videoList;
	}


	public void setVideoList(List<Video> videoList) {
		this.videoList = videoList;
	}


	public List<Topic> getTopicList() {
		return topicList;
	}


	public void setTopicList(List<Topic> topicList) {
		this.topicList = topicList;
	}


	public static List<User> getAllUsers() 
	{
		return User.findAll();	
	}


	public String getFacebookID() 
	{
		return facebookID;
	}


	public void setFacebookID(String facebookID) 
	{
		this.facebookID = facebookID;
	}

	public static User getByFacebookID(String facebookID) 
	{
		return User.find("facebookID = ? ", facebookID).first();
	}


	public String getName() {

		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setProfilPicture(String profilPicture) {
		this.profilPicture = profilPicture;
	}
	
	public String getProfilPicture() {
		return profilPicture;
	}


	public List<Serie> getFollows() {
		return follows;
	}

	public List<Rate> getRates() {
		return rates;
	}

	public List<Comment> getComments() {
		return comments;
	}
	
	public void addSeries(Serie series){
		if(!follows.contains(series)){
			follows.add(series);
		}
	}
	
	public void removeSeries(Serie series){
		if(follows.contains(series)){
			follows.remove(series);
		}
	}

	public User addComment(Serie series, String comment) {

        CommentSerie newComment = new CommentSerie(this, comment, series);
	    this.comments.add(newComment);
	    this.save();
	    return this;
	}
	
	public User removeComment(Comment comment) {
	    this.comments.remove(comment);
	    this.save();
	    return this;
	}
	
	public User addComment(Episode episode, String comment) {

        CommentEpisode newComment = new CommentEpisode(this, comment, episode);
	    this.comments.add(newComment);
	    this.save();
	    return this;
	}
	
	public User addCommentToTopic(Topic topic, String comment) {

        CommentTopic newComment = new CommentTopic(this, comment, topic);
	    this.comments.add(newComment);
	    this.save();
	    return this;
	}
	
	public User addCommentToVideo(Video video, String comment) {

        CommentVideo newComment = new CommentVideo(this, comment, video);
	    this.comments.add(newComment);
	    this.save();
	    return this;
	}
	
	public User addVideo(Video video) {

	    videoList.add(video);
	    this.save();
	    return this;
	}
	
	public User deleteVideo(Video video) {

	    videoList.remove(video);
	    this.save();
	    return this;
	}
	
	public User addTopic(Serie series, String topicName, String content, Date creationDate) {

        Topic newTopic = new Topic(topicName, creationDate, content, this, series);
	   // this.topicList.add(newTopic);
	    //this.save();
        newTopic.save();
        series.getTopicList().add(newTopic);
        series.save();
	    return this;
	}

	public User removeTopic(Topic topic) {
	    this.topicList.remove(topic);
	    this.save();
	    return this;
	}
	
	public void addRate(Rate newRate) {

        rates.add(newRate);
        
	}
	
	public void removeRate(Rate Rate) {

        rates.remove(Rate);
        
	}

}